package com.wuwei.elearning.config;

import com.wuwei.elearning.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    /**
     * 拦截器配置，登录，图片有关的信息不拦截
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> list = new ArrayList<>();
        list.add("/elearning/word/upload");
        list.add("/elearning/word/preview");
        list.add("/elearning/user/login");
        list.add("/elearning/user/logout");
        list.add("/elearning/user/uploadAvatar");
        list.add("/elearning/subject/upload/**");
        list.add("/elearning/subject/download");
        list.add("/elearning/question/upload");
        list.add("/elearning/subject/css/**");
        list.add("/elearning/subject/js/**");
        list.add("/elearning/subject/uploadPPT");
        list.add("/elearning/audio/upload");
        list.add("/elearning/audio/stream");
        list.add("/elearning/video/uploadVideo");
        list.add("/elearning/video/stream");
        list.add("/elearning/ppt/**");
        list.add("/elearning/question/getTemplate");
        list.add("/js/**");
        list.add("/css/**");
        list.add("/favicon.ico");


        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(list); // 排除指定路径
    }


    /**
     * 这里主要为了解决跨域问题,所以重写addCorsMappings方法
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(false).maxAge(3600);
    }




//    //创建了一个 ResourceHttpRequestHandler bean,并将其存放位置设置为 "file:E:\\audio\\"
//    @Bean
//    public ResourceHttpRequestHandler resourceHttpRequestHandler() {
//        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
//        handler.setLocations(Collections.singletonList(new FileSystemResource(UPLOAD_FOLDER)));
//        return handler;
//    }


//    /**
//     * 配置资源访问
//     * @param registry
//     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler(UPLOAD_DICTIONARY+"**")
//                .resourceChain(true)
//                .addResolver(new PathResourceResolver());
//    }


}
